<?php
session_start();
//error_reporting(0);
if(!isset($_SESSION['userId']))
{
	header("location:index.php");
}
include("../class/config.php");
include("../class/cls_banner.php");
$allow_ext = array('jpg', 'JPG', 'jpeg', 'JPEG', 'png', 'PNG');
if(isset($_POST['send'])=="add" && $_POST!=NULL)
{
	$banner_name=$_POST['banner_name'];
	//moveing image
	$banner_image= $_FILES['banner_image']['name'];
	//imgae uploading
	move_uploaded_file($_FILES['banner_image']['tmp_name'],'../uploads/banner/'.$_FILES['banner_image']['name']);
	$banner_status=$_POST['status'];
	$banner_date= date('y-m-d');
	if(($_GET['action'])=='add')
	{
		if(!$_POST['banner_name']==''&&!$_POST['banner_image']=='' &&!$_FILES['banner_image']['name']==''){
			$banner_image = $_FILES['banner_image']['name'];
	$ext_array = explode(".", $banner_image);
	$ext = array_pop($ext_array);
			if (in_array($ext, $allow_ext)){ 
					$result= $objBanner ->insertBanner($banner_name, $banner_image, $banner_status, $banner_date);
			}
			else $success=" <span class='errormsg'>Process Failed !!</span>";
		}
	} 
	else if(($_GET['action'])=='edit'){
		
		$banner_id= $_GET['editId'];
		if(!$_POST['banner_name']=='' &&!$_FILES['banner_image']['name']==''){
		$result= $objBanner -> updateBanner($banner_id, $banner_name, $banner_image, $banner_status, $banner_date);
		}
		if(isset($result)){
			header("location:banner.php");
		}
		}
	
	if(isset($result)){
		$success= " <span class='successmsg'>Process completed</span>";
		if(isset($_GET['action']) && $_GET['action']=='edit'){
			echo "$success";
		}
	}
	if(!isset($result)){
		$success= " <span class='errormsg'>Process Failed !! Check the informaton below</span>";
	}
		
}
if(isset($_GET['deleteId']))
{
	$bannerId=$_GET['deleteId'];
	$objBanner->deleteBanner($bannerId);
	}
if(isset($_GET['status']))
{
	$status=$_GET['status'];
	$bannerID=$_GET['bannerId'];
	$objBanner->bannerStatus($status,$bannerID);
}	

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Banner</title>
<link rel="stylesheet" href="../css/layout.css" />
</head>
<body>
	<div class="mainWrapper">
     <?php include("../includes/adminHeader.php");?>
    <div class="bodyContent">
    <h1>Banner</h1>
    <div class="innerBody">
    <?php if(!isset($_GET['action']))
	{?>
    <strong>Banner Listing</strong>  | <a href="?action=add">Add Banner</a>
  
       <?php
    	if(isset($result)){
    		$objBanner->successmsg();
    	}
		?>
      <table width="100%" border="0" cellpadding="1" cellspacing="1" class="table">
        <tr>
          <th>Sn</th>
          <th>Banner Name</th>
          <th>Banner Image</th>
          <th>Banner Status</th>
          <th>Added Date</th>
          <th>Action</th>
        </tr>
		<?php $resBanner = $objBanner->selectBanner(2,2);
		$i=1;
		while($recBanner=mysqli_fetch_array($resBanner))
		{
		?>
        <tr>
          <td><?php echo $i++;?></td>
          <td><?php echo $recBanner['banner_name'];?></td>
         <?php if($recBanner['banner_image']!='') {?>
          <a href="../uploads/banner/<?php echo $recBanner['banner_image'];?>">
          <td><img src="../uploads/banner/<?php echo $recBanner['banner_image'];?>" height="40" width="80"/></td></a>
          <?php }
         else {?>
                <a href="../uploads/banner/noimage.php">
          			<td><img src="../uploads/banner/noimage.php" height="40" width="80"/></td></a>
         <?php } ?>
		  <td>
		  <?php if($recBanner['banner_status']=="Y")
		  {
		  	?>
		  	<a href="?status=N&bannerId=<?php echo $recBanner['banner_id'];?>">
		  	<img src="../images/active.jpg"  />
		   </a>
		   <?php
		   }
		   else{
		   	?>
		   	<a href="?status=Y&bannerId=<?php echo $recBanner['banner_id'];?>">
		   <img src="../images/inactive.jpg"  />
		   </a>
		   <?php
		   }
		  ?>
		 </td>
		 <td><?php echo $recBanner['banner_added_date'];?></td>
		  
         <td><a href="?action=edit&editId=<?php echo $recBanner['banner_id'];?>">Edit</a> | 
          			<a href="?deleteId=<?php echo $recBanner['banner_id'];?>"
          	 				onclick="javascript:return confirm('Are You Sure You Want to Delete this Record?')">Delete</a>
			<a href="?editId=<?php echo $recBanner['banner_id']?>"></a></td>
        </tr>
        <?php }?>
       </table>
     <?php }
	  else if(isset($_GET['action']) && $_GET['action']=='add'or (isset($_GET['action']) && $_GET['action']=='edit') )
	 {
	 ?>
      <strong><?php echo $_GET['action']=="add"?"Add Banner":"Edit Banner" ?></strong>
      <?php 
	  if(isset($success))
      {
      echo $success;
      } ?>
      <form id="form1" name="form1" method="post" action=""enctype="multipart/form-data">
        <table width="100%" border="0" class="table" cellpadding="1" cellspacing="1">
        <?php 
        	if(($_GET['action'])=='edit'){
				$banner_id= $_GET['editId'];
        		$res=$objBanner->selectBannerById($banner_id);
				 $result=(mysqli_fetch_array($res));
        		}?>
          <tr>
            <td width="30%">Banner Name *</td>
            <td><input type="text" name="banner_name" id="banner_name" class="textBox" 
            	value="<?php if(($_GET['action'])=='edit') echo $result['banner_name']  ?>"/>
            	<?php if(isset($_POST['banner_name']) && $_POST['banner_name']==""){
            		echo "<span class='errormsg'>empty banner name !</span>";
            	} ?></td>
          </tr>
          <tr>
            <td width="30%">image *</td>
            <td><input type="file" name="banner_image" id="banner_image" 
            	value="<?php if(($_GET['action'])=='edit') echo $result['banner_image']?>"/>
            	<?php if(isset($_FILES['banner_image']['name']) &&$_FILES['banner_image']['name']==""){
            		echo "<span class='errormsg'>no file chosen !</span>";
            	}
            	if(isset($_FILES['banner_image']['name']) &&!$_FILES['banner_image']['name']==""){
				$banner_image=$_FILES['banner_image']['name'];
				
            	$ext_array = explode(".", $banner_image);
				$ext = array_pop($ext_array);
				
				if(!in_array($ext, $ext_array)){
					echo "<span class='errormsg'>invalid formate !</span>";
				}}?></td>
          </tr>
		    <tr>
			  <td width="30%">Status</td>
			 <td>
			   <input type="radio" name="status" value="Y" checked="checked" /> Active
				 <input type="radio" name="status" value="N" /> Inactive
			 </td>
			 </tr>
		 <tr>
        	<td>&nbsp;</td>
            <td><input type="submit" name="send" id="add" value="<?php echo $_GET['action']=="add"?"  Add  ":"  Edit  "?> "/>
            	<input type="button" name="cancel" value="cancel" onclick="javascript:location.href='banner.php'" /></td>
          </tr>
        </form>
       <?php
      } ?>
    </div>
   </div>
     <?php 
    if('action'==""){
    include("../includes/adminFooter.php");
    }?>
   </div>
 </body>
</html>